//
//  MYGWithdrawCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/18.
//

import UIKit

class MYGWithdrawCell: UITableViewCell {
    // MARK: - Property
    var model: MYGChatWithdrawCellModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            hintLabel.text = "\(model.isUsers ? "你" : "对方")撤回了一条消息"
            editButton.isHidden = !(model.isUsers && model.message != nil)
        }
    }
    
    var withDrawAction: ((String)->Void)?
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.addSubview(stackView)
        stackView.addArrangedSubviews([hintLabel, editButton])
        stackView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().inset(0)
            make.centerX.equalToSuperview()
        }
    }
    // MARK: - lazy load
    private lazy var stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.spacing = 9
        stackView.alignment = .center
        return stackView
    }()
    
    private lazy var hintLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(104, 104, 104, 1)
        label.text = "你撤回了一条消息"
        return label
    }()
    
    private lazy var editButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("重新编辑", for: .normal)
        btn.setTitleColor(.rgba(0, 77, 224, 1), for: .normal)
        btn.titleLabel?.font = .regular(12)
        btn.addTarget(self, action: #selector(withDraw), for: .touchUpInside)
        return btn
    }()
    
    @objc
    private func withDraw() {
        guard let text = model?.message else {
            return
        }
        
        withDrawAction?(text)
    }
}
